package me.linczh.bank.service;

import java.util.List;

import me.linczh.bank.bean.AccountBean;
import me.linczh.bank.bean.OperatorBean;
import me.linczh.bank.dao.DaoFactory;
import me.linczh.bank.dao.OperatorDao;
import me.linczh.framework.query.Query;

public class OperatorService {
	private OperatorDao operatorDao = DaoFactory.getOperatorDao();
	
	public OperatorBean create(AccountBean account, String identity,
			String username, String password) {
		OperatorBean operator = new OperatorBean();
		operator.setAccount(account.getAccount());
		operator.setIdentity(identity);
		operator.setPassword(password);
		operator.setUsername(username);
		operatorDao.create(operator);
		return null;
	}
	
	public OperatorBean getOperatorByAccountAndPassword(final String account, final String password) {
		List<OperatorBean> list = operatorDao.query(new Query<OperatorBean>() {
			String a = account;
			String p = password;
			@Override
			public boolean matched(OperatorBean item) {
				// TODO Auto-generated method stub
				if (item.getAccount().equals(a) && item.getPassword().equals(p)) return true;
				return false;
			}
			@Override
			public String statement() {
				// TODO Auto-generated method stub
				return "select * from operator_tbl where " +
						"account='" + a + "' and " +
						"password='" + p + "'";
			}
		});
		if (list.size() != 0) return list.get(0);
		return null;
	}
	
	public List<OperatorBean> getOperatorByAccount(final String account) {
		List<OperatorBean> list = operatorDao.query(new Query<OperatorBean>() {
			String a = account;
			@Override
			public boolean matched(OperatorBean item) {
				// TODO Auto-generated method stub
				if (item.getAccount().equals(a)) return true;
				return false;
			}
			@Override
			public String statement() {
				// TODO Auto-generated method stub
				return "select * from operator_tbl where " +
						"account='" + a + "'";
			}
		});
		return list;
	}
	
	public boolean delete(final String account, final String identity) {
		List<OperatorBean> list = operatorDao.query(new Query<OperatorBean>() {
			String a = account;
			String i = identity;
			@Override
			public boolean matched(OperatorBean item) {
				// TODO Auto-generated method stub
				if (item.getAccount().equals(a) && item.getIdentity().equals(i)) return true;
				return false;
			}
			@Override
			public String statement() {
				// TODO Auto-generated method stub
				return "select * from operator_tbl where " +
						"account='" + a + "' and " +
						"identity='" + i + "'";
			}
		});
		if (list != null) {
			operatorDao.delete(list.get(0));
			return true;
		}
		return false;
	}

}
